package net.zoneland.geological.devicedata.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import net.zoneland.common.excel.reader.ExcelReaderUtil;
import net.zoneland.common.excel.reader.IRowReader;
import net.zoneland.geological.devicedata.controller.excelreader.DeepMoveDataImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.FlowDataImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.PressureDataImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.RainDataImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.SurfaceMoveDataImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.SurveyDataImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.TestImportExcelReader;
import net.zoneland.geological.devicedata.controller.excelreader.WaterDataImportExcelReader;
import net.zoneland.greenleaf.system.controller.BaseController;

/**
 * 勘察数据导入控制器(Controller|Action)
 * 作   者：李义<br/>
 * 日   期：2017-10-23 13:34:07
**/
@Controller
@RequestMapping("surveydata")
public class SurveyDataImportController extends BaseController { 

	/**
	 * 如果需要系统对此Controller的访问进行监控，记录服务访问的时间，耗时，那么请将monitor属性设置为“true”
	 */

	public boolean monitor = false; 

	private static Logger logger = Logger.getLogger(SurveyDataImportController.class);

	//获取类的名称
	@SuppressWarnings("unused")
	private String clazzName = new Object() {
		public String getClassName() {
			String clazzName = this.getClass().getName();
			return clazzName.substring(0, clazzName.lastIndexOf('$'));
		}
	}.getClassName();


	/**
	 * 上传并且导入数据
	 * 
	 * @param entity
	**/
	@RequestMapping(value="/import", method = RequestMethod.POST,  consumes = "multipart/form-data" )
	@ResponseBody
	public Object importExcelData( @RequestParam("file") CommonsMultipartFile file ){
		Map<String, Object> map = new HashMap<String, Object>();
		// 判断文件是否存在
		String originalFilename = null;
		IRowReader reader = null;
		InputStream is = null;
        if (!file.isEmpty()) {
	        	//获取到一个输入流
        	try {
        		is = file.getInputStream();
	        	originalFilename = file.getOriginalFilename();
	        	
	        	//这里可以根据传入的参数来判断一下，如果不同的数据格式，可以实例化不同的Reader
	        	//reader统一放在net.zoneland.geological.devicedata.controller.excelreader包里
	        	reader = new SurveyDataImportExcelReader();
	        	
	        	logger.info( ">>>>>>>>>>>>>系统开始读取并且导入数据文件......" );
	        	
	        	ExcelReaderUtil.readExcel( reader, is, originalFilename, 1, null);
				
	        	map.put("result", "success");
	        	map.put("message", "数据文件导入完成!");
			} catch (IOException e) {
				map.put("result", "error");
				map.put("message", "系统未成功获取到数据文件的文件流!");
				logger.error( "系统未成功获取到数据文件的文件流。", e );
			} catch (Exception e) {
				map.put("result", "error");
				map.put("message", "系统解析数据文件时发生未知异常!");
				logger.error( "系统解析数据文件时发生未知异常。", e );
			}        	
        }else {
        	map.put("result", "error");
			map.put("message", "系统未发现需要解析的数据文件!");
			logger.error( "系统未发现需要解析的数据文件。" );
        }
		return map;
	}

}
